namespace PickGold.Charting
{
    using System;
    using System.ComponentModel;
    using System.Globalization;

    internal class DoubleArrayConverter : ArrayConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object obj2 = null;
            bool flag = false;
            if ((context != null) && (context.Instance != null))
            {
                DataPoint instance = (DataPoint) context.Instance;
                if ((instance.series != null) && instance.series.IsYValueDateTime())
                {
                    flag = true;
                }
            }
            string str = value as string;
            if (str == null)
            {
                return base.ConvertFrom(context, culture, value);
            }
            string[] strArray = str.Split(new char[] { ',' });
            double[] numArray = new double[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                if (flag)
                {
                    DateTime time;
                    if (DateTime.TryParse(strArray[i], CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
                    {
                        obj2 = time;
                    }
                    else if (DateTime.TryParse(strArray[i], CultureInfo.CurrentCulture, DateTimeStyles.None, out time))
                    {
                        obj2 = time;
                    }
                    else
                    {
                        obj2 = null;
                    }
                }
                if (obj2 != null)
                {
                    numArray[i] = (double) obj2;
                }
                else
                {
                    numArray[i] = CommonElements.ParseDouble(strArray[i]);
                }
            }
            return numArray;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            bool flag = false;
            if ((context != null) && (context.Instance != null))
            {
                DataPoint instance = (DataPoint) context.Instance;
                if ((instance.series != null) && instance.series.IsYValueDateTime())
                {
                    flag = true;
                }
            }
            if (destinationType != typeof(string))
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
            double[] numArray = (double[]) value;
            string str = "";
            foreach (double num in numArray)
            {
                if (flag)
                {
                    str = str + DateTime.FromOADate(num).ToString("g", CultureInfo.InvariantCulture) + ",";
                }
                else
                {
                    str = str + num.ToString(CultureInfo.InvariantCulture) + ",";
                }
            }
            return str.TrimEnd(new char[] { ',' });
        }
    }
}

